<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\api\services\admin\setting\DictServices;
use app\Request;
use think\App;

class DictController extends BaseApi
{

    private DictServices $services;

    /**
     * 构造函数
     *
     * @param \app\Request                                 $request
     * @param \think\App                                   $app
     * @param \app\api\services\admin\setting\DictServices $services
     */
    public function __construct(Request $request, App $app, DictServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 查询列表
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public function select(Request $request): \think\response\Json
    {
        $where = $request->getParams([
            ['name', '', '', 'name_like'],
            ['delete_time', null],
        ]);
        $this->validate($where, [
            'name'   => 'max:32',
            'status' => 'in:0,1',
        ], [], true, true);
        if (empty($where['name_like'])) {
            $where['name_like'] = 'dict_';
        }

        $ret = $this->services->select($where);
        return ret_value(0, "ok", $ret['list'], 200, [], ["count" => $ret['count']]);
    }

    /**
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function insert(Request $request): \think\response\Json
    {
        $data = $request->getParams([['name', ''], ['value', '']]);
        $this->validate($data, [
            'name'  => 'require',
            'value' => 'require',
        ], [], true, true);
        $ret = $this->services->insert($data);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    /**
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function update(Request $request): \think\response\Json
    {
        $data = $request->getParams([['name', ''], ['value', '']]);
        $this->validate($data, [
            'name'  => 'require',
            'value' => 'require',
        ], [], true, true);
        $ret = $this->services->update($data['name'], $data, 'name');
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    /**
     * 删除
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function delete(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['name'],
        ]);
        $this->validate($data, [
            'name' => 'require',
        ]);

        $this->validate($data, [
            'name' => 'max:32',
        ], [], true, true);
        $ids = is_array($data["name"]) ? $data["name"] : explode(',', $data['name']);
        $ret = $this->services->batchUpdate($ids, ['delete_time' => date("Y-m-d H:i:s")], 'name');
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    /**
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function get(Request $request): \think\response\Json
    {
        $where = $request->getParams([
            ['name'],
        ]);
        $this->validate($where, [
            'name' => 'require',
        ]);
        $list = $this->services->get($where);
        $data = isset($list['value']) ? json_decode($list['value'], 1) : [];
        return ret_value(0, "ok", $data);
    }
}
